import axios from 'axios'
import store from '@/store'

const http = axios.create({
  baseURL: 'http://geek.itheima.net/v1_0',
  timeout: 5000
})

http.interceptors.request.use(
  config => {
    const token = store.getState().user.token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

http.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response.status === 401) {
      window.localStorage.removeItem('persist:root')
      window.location.reload()
    }

    return Promise.reject(error)
  }
)

export default http
